﻿-- =============================================
-- Author:		<赵洪涛>
-- Create date: <2012-02-03>
-- Description:	<大区人资获取订单数据>
-- =============================================
CREATE PROCEDURE [dbo].[proc_Order_GetItem2] 
	@OId uniqueidentifier,
	@CompanyId varchar(10)
AS
Begin
	Declare @tmpStateName nvarchar(20),@tmpStateId int
	Set @tmpStateName = '未知'

	Select @tmpStateId=StateId From [Order]	Where OId=@OId And CompanyId=@CompanyId
	IF @tmpStateId Is Not Null
		Select @tmpStateName=IsNull(Title,'未知') From [Order_State] Where [Order_State].Id=@tmpStateId

	Select [Order].Id
		,OId
		,OrderId
		,CusId
		,CusName
		,CusPerson
		,CusPersonTel
		,CusDescription
		,CusEstimate
		,[Order].TypeId
		,TypeName
		,WayId
		,WayName
		,ProcessId
		,ProcessName
		,Salesman
		,SalesmanAccount
		,TradeDate
		,[Order].CreateDate
		,IsNull(shjbh,'') As shjbh
		,IsNull(htbh,'') As htbh
		,YingShouPrice
		,ISNULL(ShiShouPrice,0) As ShiShouPrice
		,Discount
		,PaymentTypeId
		,PaymentType
		,BankId
		,Bank
		,PaymentDescription
		,OtherDescription
		,DepId
		,[Order].CompanyId
		,[Order].StateId
		,IsNull(TrackKfName,'') As TrackKfName
		,GenJinKfName
		,IsNull([Order].AuditPerson,'') As AuditPerson
		,IsNull(AuditDate,'1900-01-01') As AuditDate
		,NOE_Flag
		,@tmpStateName As StateName,c.RegTime,c.RegCapital,c.Employees
		,(Select Title from Customer_Employees e where e.id=c.Employees) EmployeesName,c.Introduction,p.Age,p.Weixin
	From [Order] left join Customer c on [Order].CusId=c.CustomerId 
		 left join ContactPerson p on [Order].CusId=p.CustomerId and p.IsMain=1
	Where OId=@OId And [Order].CompanyId=@CompanyId
End